{extend name="iframe" /}

{block name="body"}
<style>
    .layui-upload-img {
        width: 92px;
        height: 92px;
        margin: 0 10px 10px 0;
    }
</style>
<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
    <form action="{:request()->url()}" method="post" class="xn_ajax" data-type="open">
        <div class="layui-form-item">
            <label class="layui-form-label">行业类型</label>
            <div class="layui-input-inline">
                <select name="parent_typeid" lay-filter="parent_typeid" id="parent_typeid">
                    <option value="0">请选择</option>
                    {foreach name="parent_typelist" item="vo"}
                    <option value="{$vo.id}" {if condition="$data.parent_typeid eq $vo.id"}selected{/if} >{$vo.title}</option>
                    {/foreach}
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="typeid" id="typeid">
                    <option value="0">请选择</option>
                    {foreach name="typelist" item="vo"}
                    <option value="{$vo.id}" {if condition="$data.typeid eq $vo.id"}selected{/if} >{$vo.title}</option>
                    {/foreach}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属项目</label>
            <div class="layui-input-block layui-form-select">
                <input type="text" name="project" placeholder="" autocomplete="off" class="layui-input" value="{$data.project}" id="project">
                <dl class="layui-anim layui-anim-upbit" id="projectList">
                </dl>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">图片</label>
            <div class="layui-input-block">
                <img src="{$data.url}" style="max-height: 320px; width: auto;">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" lay-submit style="display: none;" id="save"></button>
                <button type="button" class="layui-btn" id="submit">保存</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
{/block}
{block name="js"}
<script>
    var form = layui.form;
    form.on('select(parent_typeid)', function(res) { 
        $("#typeid").empty();
        if(res.value != 0){
            $.get("{:url('admin/DataType/getSonType')}?pid="+res.value,function(result){
                if(result.status){
                    $("#typeid").append(new Option('请选择',0));
                    $.each(result.data,function(index,item){
                        $("#typeid").append(new Option(item.title,item.id));
                    });
                    form.render("select");
                }
            });
        }else{
            $("#typeid").append(new Option('请选择',0));
            form.render("select");
        }
    })

    $(function(){
       $('#project').keyup(function(event){
            var word = $("#project").val().trim();
            if(word.length>0){
                $.get("{:url('admin/DataProject/getProjectByLike')}",{word:word},function(res){
                    if(res.status){
                        var html = '';
                        $.each(res.data,function(index,obj){
                            html += '<dd data-tid="'+obj.typeid+'" data-ptid="'+obj.parent_typeid+'">'+obj.title+'</dd>';
                        });
                        $('#projectList').html(html).css('display','block');
                    }else{
                        $('#projectList').html('').css('display','none');
                    }
                });
            }else{
                $('#projectList').html('').css('display','none');
            }
       });

       $('#projectList').on('click','dd',function(){
           var ptid = $(this).attr('data-ptid');
           var tid = $(this).attr('data-tid');

           $('#project').val($(this).text());
           $('#projectList').html('').css('display','none');
           
           var dd = $('#parent_typeid').next().find('dl dd[lay-value='+ptid+']');
           dd.click();

            $.get("{:url('admin/DataType/getSonType')}?pid="+ptid,function(result){
                if(result.status){
                    $("#typeid").empty();
                    $("#typeid").append(new Option('请选择',0));
                    $.each(result.data,function(index,item){
                        if(tid == item.id){
                            $("#typeid").append(new Option(item.title,item.id,false,true));
                        }else{
                            $("#typeid").append(new Option(item.title,item.id));
                        }
                    });
                    form.render("select");
                }
            });
           
       });
       $('#project').click(function(){
            if( $("#projectList").children().length ){
                $("#projectList").toggle();
            }
       });

       $('#submit').click(function(){
            var word = $("#project").val().trim();
            if(word.length>0){
                $.get("{:url('admin/DataProject/getProjectByName')}",{word:word},function(res){
                    if(res.status){
                        $('#save').click();
                    }else{
                        layer.msg('此项目不存在',{time:1500,icon:2});
                    }
                });
            }else{
                $('#save').click();
            }
       });
   });
</script>
{/block}